﻿using Coldairarrow.Entity.DTO;
using Coldairarrow.Entity.Goods;
using Coldairarrow.Entity.PersonalCenter;
using Coldairarrow.IBusiness;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace Coldairarrow.Business.PersonalCenter
{
    public class BTB_CollectionBusiness : BaseBusiness<BTB_Collection>, IBTB_CollectionBusiness, ITransientDependency
    {
        readonly IOperator _operator;
        public BTB_CollectionBusiness(IDbAccessor db, IOperator @operator)
            : base(db)
        {
            _operator = @operator;
        }

        #region 外部接口

        public async Task<PageResult<BTB_GoodsDto>> GetDataListAsync(PageInput<BTB_GoodsDto> input)
        {
            var browse = input.Search;
            var result = GetIQueryable().Where(b => b.Userid == _operator.UserId);
            var where = LinqHelper.True<BTB_Goods>();
            int count = result.Count();
            //排除不存在的商品
            result = from a in result
                     join b in Db.GetIQueryable<BTB_Goods>() on a.Goodsid equals b.ID
                     select a;
            var q = await result.Select(s => new BTB_GoodsDto
            {
                BId = s.id,
                ID = s.Goodsid,
                GoodsName = Db.GetIQueryable<BTB_Goods>(true).Where(v => v.ID == s.Goodsid).FirstOrDefault().GoodsName,
                GoodsDescribe = Db.GetIQueryable<BTB_Goods>(true).Where(v => v.ID == s.Goodsid).FirstOrDefault().GoodsDescribe,
                GoodsPic = Db.GetIQueryable<BTB_Goods>(true).Where(v => v.ID == s.Goodsid).FirstOrDefault().GoodsPic,
                GoodsPrices = Db.GetIQueryable<BTB_GoodsSpecificationsItem>(true).Where(z => z.GoodsID == s.Goodsid && z.IsSale == true).FirstOrDefault().Price,
                BTB_GoodsSpecificationsItemID = Db.GetIQueryable< BTB_GoodsSpecificationsItem >(true).Where(i=>i.IsSale == true).FirstOrDefault().GoodsSpecificationsItemID,
                Inventory = Db.GetIQueryable<BTB_GoodsSpecificationsItem>(true).Where(z => z.GoodsID == s.Goodsid && z.IsSale == true).FirstOrDefault().Inventory,
            }).OrderByDescending(s =>s.BId).Skip((input.PageIndex - 1) * input.PageRows)
                .Take(input.PageRows)
                .ToListAsync();

            return new PageResult<BTB_GoodsDto> { Data = q, Total = count };
        }

        public async Task AddDataAsync(BTB_Collection data)
        {
            BTB_Collection data1 = new BTB_Collection()
            {
                id = GuidHelper.GenerateKey(),
                Userid = _operator.UserId,
                CreatorId = data.CreatorId,
                Goodsid = data.Goodsid,
                CreateTime = data.CreateTime,
                Editor = data.Editor,
                EditTime = data.EditTime,
                Deleted = data.Deleted
            };
            var count = GetIQueryable().Where(q => q.Userid == _operator.UserId && q.Goodsid == data1.Goodsid).ToList().Count();
            if (count > 0)
            {
                throw new BusException("已在收藏中", errorCode: -1);
            }
            else
            {
                await InsertAsync(data1);
            }
        }

        public async Task UpdateDataAsync(BTB_Collection data)
        {
            await UpdateAsync(data);
        }

        public async Task DeleteDataAsync(List<string> ids)
        {
            foreach (var item in ids)
            {
                var data = GetIQueryable().Where(q => q.Userid == _operator.UserId && q.Goodsid == item).FirstOrDefault().id;
                await DeleteAsync(data);
            }
        }
        /// <summary>
        /// 批量加入收藏
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public async Task BatchAdd(List<BTB_browse> data)
        {
            foreach (var item in data)
            {
                BTB_Collection data1 = new BTB_Collection()
                {
                    id = GuidHelper.GenerateKey(),
                    Userid = _operator.UserId,
                    CreatorId = item.CreatorId,
                    Goodsid = item.Goodsid,
                    CreateTime = item.CreateTime,
                    Editor = item.Editor,
                    EditTime = item.EditTime,
                    Deleted = item.Deleted
                };
                var count = GetIQueryable().Where(q => q.Userid == _operator.UserId && q.Goodsid == data1.Goodsid).ToList().Count();
                if (count > 0)
                {
                    throw new BusException("已在收藏中", errorCode: -1);
                }
                else
                {
                    await InsertAsync(data1);
                }
            }
        }

        #endregion

        #region 私有成员

        #endregion
    }
}